package com.fzu.ess.ess.service.Impl;

import com.fzu.ess.common.Const;
import com.fzu.ess.common.Model.JsonModel;
import com.fzu.ess.ess.dao.FeedbackDao;
import com.fzu.ess.ess.entity.Feedback;
import com.fzu.ess.ess.service.FeedbackService;
import com.fzu.ess.sys.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by ercha on 2015/10/25.
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public JsonModel create(Feedback feedback) {
        JsonModel j = new JsonModel();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User)session.getAttribute(Const.SESSION.USER);
        if(user==null){
            j.setMsg("请先登录！");
            return j;
        }
        try {
            feedback.setCreator(user);
            j.setSuccess(null != feedbackDao.save(feedback));
        } catch (Exception e){
            e.printStackTrace();
        }
        return j;
    }

    @Override
    public JsonModel getOwnByPage(Integer pageNumber, Integer pageSize) {
        JsonModel j = new JsonModel();

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User)session.getAttribute(Const.SESSION.USER);

        StringBuilder hqlData = new StringBuilder("from Feedback b where b.creator.id = '"+ user.getId() + "' "
                                                    + " and b.delFlag = " + Const.DEL_FLAG_NORMAL);
        String hqlCount;

        hqlData.append(" order by b.createDate desc");
        if(pageNumber==null || pageNumber<0){
            pageNumber = 0;
        }

        if(pageSize==null || pageSize<0){
            pageSize = 0;
        }

        hqlCount = "select count(*) " + hqlData.toString();
        try {
            List<Feedback> bl = feedbackDao.find(hqlData.toString(), pageNumber, pageSize);
            j.setDataObj(bl);
            j.setTotal(feedbackDao.count(hqlCount));
            j.setRows(bl.size());
            return j;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JsonModel getPageData(Integer pageNumber, Integer pageSize) {
        JsonModel j = new JsonModel();

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User)session.getAttribute(Const.SESSION.USER);

        if(user.getLevel()>1){
            return j;
        }

        StringBuilder hqlData = new StringBuilder("from Feedback b ");
        String hqlCount;

        hqlData.append(" where b.delFlag=" + Const.DEL_FLAG_NORMAL + " order by b.createDate desc");
        if(pageNumber==null || pageNumber<0){
            pageNumber = 0;
        }

        if(pageSize==null || pageSize<0){
            pageSize = 0;
        }

        hqlCount = "select count(*) " + hqlData.toString();
        try {
            List<Feedback> bl = feedbackDao.find(hqlData.toString(), pageNumber, pageSize);
            j.setDataObj(bl);
            j.setTotal(feedbackDao.count(hqlCount));
            j.setRows(bl.size());
            return j;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
